﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Typheus
{
    public partial class MainForm : Form
    {
        private List<Tab> Tabs=new List<Tab>();
        private bool chec;
        private int x = 0;
        public MainForm()
        {
            InitializeComponent();
            this.WindowState = FormWindowState.Maximized;
            //web.Visible = false;
            inactiveBackground.Visible = true;
            inactiveBackground.Dock = DockStyle.Fill;
            //web.BackgroundImage = global::Typheus.Properties.Resources.background;
            chec = ReadXML.GetDebugMode();
        }

        private void CreateTab(string URL)
        {
            WebBrowser newWeb = new WebBrowser();
            newWeb.AllowWebBrowserDrop = true;
            newWeb.Navigated += new WebBrowserNavigatedEventHandler(web_Navigated);
            //newWeb.Navigating += new WebBrowserNavigatingEventHandler(newWeb_Navigating);
            if (chec)
            {
                newWeb.ScriptErrorsSuppressed = false;
            }
            else if (!chec)
            {
                newWeb.ScriptErrorsSuppressed = true;
            }
            newWeb.Navigate(URL);
            newWeb.Dock = DockStyle.Fill;
            int TabIndex = tabs.TabCount;
            TabPage newPage = new TabPage();
            newPage.Text = newWeb.DocumentTitle;
            newPage.Name = "Tab" + x.ToString();
            x++;
            newPage.Controls.Add(newWeb);
            tabs.TabPages.Add(newPage);
            Tabs.Add(new Tab(newWeb, TabIndex, newPage.Name));
            
        }

        void newWeb_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
            WebBrowser web = GetSelectedBrowser();
            tabs.SelectedTab.Text = web.DocumentTitle;
            txtUrl.Text = web.Url.AbsoluteUri;
        }

        private void CreateTab()
        {
            WebBrowser newWeb=new WebBrowser();
            newWeb.AllowWebBrowserDrop = true;
            newWeb.Navigated += new WebBrowserNavigatedEventHandler(web_Navigated);
            if (chec)
            {
                newWeb.ScriptErrorsSuppressed = false;
            }
            else if (!chec)
            {
                newWeb.ScriptErrorsSuppressed = true;
            }
            newWeb.Dock = DockStyle.Fill;
            int TabIndex = tabs.TabCount;
            TabPage newPage = new TabPage();
            newPage.Name = "Tab" + x.ToString();
            newPage.Controls.Add(newWeb);
            tabs.TabPages.Add(newPage);
            Tabs.Add(new Tab(newWeb,TabIndex,newPage.Name));
        }

        private WebBrowser GetSelectedBrowser()
        {
            int selected = tabs.SelectedIndex;
            WebBrowser web1 = new WebBrowser();
            foreach (Tab t in Tabs)
            {
                if (t.TabName == tabs.SelectedTab.Name)
                {
                    web1 = t.web;
                }
            }
            return web1;
        }

        private void web_Click(object sender, EventArgs e)
        {
            WebBrowser web = GetSelectedBrowser();
            tabs.SelectedTab.Text = web.DocumentTitle;
            txtUrl.Text = web.Url.AbsoluteUri;
        }

        private void btnGo_Click(object sender, EventArgs e)
        {
            inactiveBackground.Visible = false;
            if (tabs.TabCount < 1)
            {
                tabs.Visible = true;
                tabs.Dock = DockStyle.Fill;
                CreateTab(txtUrl.Text);
            }
            else
            {
                WebBrowser web1 = GetSelectedBrowser();
                web1.Visible = true;
                web1.Dock = DockStyle.Fill;
                web1.Navigate(txtUrl.Text);
            }
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            WebBrowser web = GetSelectedBrowser();
            tabs.SelectedTab.Text = web.DocumentTitle;
            web.GoBack();
        }

        private void btnForward_Click(object sender, EventArgs e)
        {
            WebBrowser web = GetSelectedBrowser();
            tabs.SelectedTab.Text = web.DocumentTitle;
            web.GoForward();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            WebBrowser web = GetSelectedBrowser();
            tabs.SelectedTab.Text = web.DocumentTitle;
            web.Stop();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            WebBrowser web = GetSelectedBrowser();
            tabs.SelectedTab.Text = web.DocumentTitle;
            web.Refresh();
        }

        private void btnSettings_Click(object sender, EventArgs e)
        {
            WebBrowser web = GetSelectedBrowser();

           Settings s =new Settings(web);
            s.Show();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnMiniize_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void btnHome_Click(object sender, EventArgs e)
        {
            if (tabs.TabCount < 1)
            {
                tabs.Visible = true;
                tabs.Dock = DockStyle.Fill;
                CreateTab(ReadXML.GetHomepage());
            }
            else
            {
                WebBrowser web = GetSelectedBrowser();
                inactiveBackground.Visible = false;
                web.Visible = true;
                web.Dock = DockStyle.Fill;
                web.Navigate(ReadXML.GetHomepage());
                //txtUrl.Text = Properties.Settings.Default["HomePage"].ToString();
            }
        }

        private void web_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            WebBrowser web = GetSelectedBrowser();
            tabs.SelectedTab.Text = web.DocumentTitle;
            txtUrl.Text = web.Url.AbsoluteUri;
        }

        private void txtUrl_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {

                inactiveBackground.Visible = false;
                if (tabs.TabCount < 1)
                {
                    tabs.Visible = true;
                    tabs.Dock = DockStyle.Fill;
                    CreateTab(txtUrl.Text);
                }
                else
                {
                    WebBrowser web1 = GetSelectedBrowser();
                    web1.Visible = true;
                    web1.Dock = DockStyle.Fill;
                    web1.Navigate(txtUrl.Text);
                    tabs.SelectedTab.Text = web1.DocumentTitle;
                }
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                WebBrowser web = GetSelectedBrowser();
                Bookmark b = new Bookmark(web.DocumentTitle, web.Url.AbsoluteUri, web);
                b.Show();
            }
            catch (NullReferenceException ex)
            {
                inactiveBackground.Visible = false;
                if (tabs.TabCount < 1)
                {
                    tabs.Visible = true;
                    tabs.Dock = DockStyle.Fill;
                    CreateTab(ReadXML.GetHomepage());
                }
                else
                {
                    WebBrowser web1 = GetSelectedBrowser();
                    web1.Visible = true;
                    web1.Dock = DockStyle.Fill;
                    web1.Navigate(ReadXML.GetHomepage());
                }
            }
        }

        private void newTabToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CreateTab();
        }

        private void closeTabToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Tab selectTab=null;
            foreach (Tab t in Tabs)
            {
                if (t.TabName == tabs.SelectedTab.Name)
                {
                    selectTab = t;
                }
            }
            if (selectTab != null)
            {
                Tabs.Remove(selectTab);
            }
            tabs.TabPages.Remove(tabs.SelectedTab);
        }
    }
}
